<?php
/*
*	Static Class for Transferring from Database
*	涉及的数据库数据处理类
*/
class Data extends Base{
	function query($sql){
		global $db;
		$query = $db->query($sql);
		while($fetchdata = $db->fetch_array($query)){
			$getdata[] = $fetchdata;
		}
		return $getdata;
	}
	function queryfirst($sql){
		global $db;
		$getdata = $db->query_first($sql);
		return $getdata;
	}
	function select($name, $cols='', $where='', $order='', $limit=''){
		global $db;
		$sql = "SELECT";
		if($cols)$sql.= " {$cols} FROM {$name}";
		if($where)$sql.= " WHERE {$where}";
		if($order)$sql.= " ORDER BY {$order}";
		if($limit)$sql.= " LIMIT {$limit}";
		//echo $sql;
		$query = $db->query($sql);
		while($fetchdata = $db->fetch_array($query)){
			$getdata[] = $fetchdata;
		}
		return $getdata;
	}
	function selectfirst($name, $cols='', $where='', $order='', $limit=''){
		global $db;
		$sql = "SELECT";
		if($cols)$sql.= " {$cols} FROM {$name}";
		if($where)$sql.= " WHERE {$where}";
		if($order)$sql.= " ORDER BY {$order}";
		if($limit)$sql.= " LIMIT {$limit}";
		//echo $sql;
		$getdata = $db->query_first($sql);
		return $getdata;
	}
	function update($name, $array, $where=''){
		global $db;
		$sql = "UPDATE {$name} SET";
		$cols = array();
		foreach($array as $k=>$v){
			$cols[] = "{$k}='{$v}'";
		}
		$sql.= " ".join(',', $cols);
		if($where)$sql.= " WHERE {$where}";
		if($db->query($sql)){
			return true;
		}else{
			return false;
		}
	}
	function insert($name, $array){
		global $db;
		$sql = "INSERT INTO {$name}";
		$cols = array_keys($array);
		$values = array_values($array);
		$sql.= "(".join(',', $cols).") values('".join("','", $values)."')";
		if($db->query($sql)){
			return true;
		}else{
			return false;
		}
	}
	function delete($name, $where){
		global $db;
		$sql = "DELETE FROM {$name}";
		$sql.= " WHERE {$where}";
		if($db->query($sql)){
			return true;
		}else{
			return false;
		}
	}
	function check($name, $where=''){
		global $db;
		$sql = "SELECT * FROM {$name}";
		$cols = array();
		foreach($where as $k=>$v){
			if(strpos($v ,'%')>-1)
				$cols[] = "{$k} LIKE '{$v}'";
			elseif(substr($k, 0, 1)=='!')
				$cols[] = substr($k, 1)."!='{$v}'";
			else
				$cols[] = "{$k}='{$v}'";
		}
		$sql.= " WHERE ".join(' and ', $cols);
		//echo $sql;
		$getdata = $db->query_first($sql);
		return $getdata ? true : false;
		exit;
	}
	function getone($table, $col, $where=''){
		global $db;
		$sql = "SELECT {$col} FROM {$table}";
		$cols = array();
		foreach($where as $k=>$v){
			if(strpos($v ,'%')>-1)
				$cols[] = "{$k} LIKE '{$v}'";
			elseif(substr($k, 0, 1)=='!')
				$cols[] = substr($k, 1)."!='{$v}'";
			else
				$cols[] = "{$k}='{$v}'";
		}
		$sql.= " WHERE ".join(' and ', $cols);
		//echo $sql;
		$gd = $db->query_first($sql);
		return $gd[$col] ? $gd[$col] : false;
	}
}
?>